<template>
  <div id="app">
    <div id="container"></div>
    <div id="panel" style="opacity: 0;"></div>
  </div>
</template>

<script>
export default {
  name: "passengerStart",
  data() {
    return {
      webSocket: {}
    }
  },
  created() {
    let _this = this
    this.webSocket = new WebSocket('wss://localhost:8813/orderSocket/' + _this.$store.state.userInfo.token);
    this.webSocket.onerror = function (event) {
      onError(event)
    };
    this.webSocket.onopen = function (event) {
      onOpen(event)
    };
    this.webSocket.onmessage = function (event) {
      onMessage(event)
    };
    function onMessage(event) {
      let msg = JSON.parse(event.data)
      if (msg && msg.ping && msg.ping != '') {

      } else {
        if (msg.msg.orderNumber === _this.$store.state.order.orderNumber) {
          _this.$store.state.order.startLongitude = _this.$store.state.order.endLongitude
          _this.$store.state.order.startLatitude = _this.$store.state.order.endLatitude
          _this.$router.replace({ path:'/empty', query: {urls: 'passengerStart'} })
        }
        if (msg.msg.pay === 1) {
          _this.$router.push('/passenger/order/end')
        }
      }
    }
    function onOpen(event) {
      console.log('Connection established')
    }
    function onError(event) {
      alert(event.data);
    }

  },
  mounted() {
    let _this = this
    //基本地图加载
    var map = new AMap.Map("container", {
      resizeEnable: true,
      center: [116.397428, 39.90923],//地图中心点
      zoom: 13 //地图显示的缩放级别
    });
    //构造路线导航类
    var driving = new AMap.Driving({
      map: map,
      panel: "panel"
    });
    // 根据起终点经纬度规划驾车导航路线
    driving.search(new AMap.LngLat(_this.$store.state.order.startLongitude, _this.$store.state.order.startLatitude), new AMap.LngLat(_this.$store.state.order.endLongitude, _this.$store.state.order.endLatitude), function(status, result) {
      // result 即是对应的驾车导航信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
      if (status === 'complete') {
        log.success('绘制驾车路线完成')
      } else {
        log.error('获取驾车数据失败：' + result)
      }
    });
  },
  destroyed() {
    this.webSocket.close()
  },
  methods: {

  }
}
</script>

<style scoped>
#panel {
  position: fixed;
  background-color: white;
  max-height: 90%;
  overflow-y: auto;
  top: 10px;
  right: 10px;
  width: 280px;
}
#panel .amap-call {
  background-color: #009cf9;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}
#panel .amap-lib-driving {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  overflow: hidden;
}
</style>
